﻿using Mapster;
using Meiam.System.Hostd.Authorization;
using Meiam.System.Hostd.Extensions;
using Meiam.System.Interfaces;
using Meiam.System.Model;
using Meiam.System.Model.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meiam.System.Hostd.Controllers.Basic
{
    /// <summary>
    /// 接口中心
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[ApiExplorerSettings(IgnoreApi = true)]
    public class ToOracleController : BaseController
    {
        /// <summary>
        /// 日志管理接口
        /// </summary>
        private readonly ILogger<ToOracleController> _logger;
        /// <summary>
        /// 会话管理接口
        /// </summary>
        private readonly TokenManager _tokenManager;

        /// <summary>
        /// 业务定义接口
        /// </summary>
        private readonly IBaseToOracleERPService _ToOracleERP;

        /// <summary>
        /// 数据关系接口
        /// </summary>
        private readonly ISysDataRelationService  _dataRelationService;


        public ToOracleController(ILogger<ToOracleController> logger, TokenManager tokenManager, IBaseToOracleERPService toOracleERPService, ISysDataRelationService dataRelationService)
        {
            _logger = logger;
            _tokenManager = tokenManager;
            _ToOracleERP = toOracleERPService;
            _dataRelationService = dataRelationService;
        }

        /// <summary>
        /// 工单完工入库接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult CompleteTOOracleERP([FromBody] string strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            CompleteResponseDto result = new CompleteResponseDto();
            string statuscode = string.Empty;
            try
            {
                result =_ToOracleERP.CompleteToERP(strparm, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            //return toResponse(StatusCodeType.Success, result+ strMsg.ToString());
            return toResponse<CompleteResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
        }

        /// <summary>
        /// 搬运单产成品入库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult ProductTOERP([FromBody] string strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            ProductResponseDto result = new ProductResponseDto();
            string statuscode =string.Empty ;
            try
            {
                result = _ToOracleERP.ProductTOERP(strparm,out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<ProductResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 搬运单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_MOVE")]
        public IActionResult MoveOrderTOERP([FromBody] string strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            MoveOrderResponseDto result = new MoveOrderResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToOracleERP.MoveOrderTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<MoveOrderResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 采购及RMA接收
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_RECEIVE")]
        public IActionResult ReceivingTOERP([FromBody] string strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            ReceivingResponseDto result = new ReceivingResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToOracleERP.ReceivingTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<ReceivingResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 订单挑库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_PICK")]
        public IActionResult PickTOERP([FromBody] string strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            PickResponseDto result = new PickResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToOracleERP.PickTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<PickResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 库存处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_TRANSACTIONS")]
        public IActionResult TransActionsTOERP([FromBody] string strparm)
        {
            StringBuilder strMsg = new StringBuilder();
            TranActionsResponseDto result = new TranActionsResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToOracleERP.TransActionsTOERP(strparm, out statuscode, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<TranActionsResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 工单发料同步ERP接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult FeedingTOOracleERP([FromBody] string stringjson)
        {
            StringBuilder strMsg = new StringBuilder();
            //string result = string.Empty;
            FeedResponseDto result = new FeedResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToOracleERP.FeedingToERP(stringjson, strMsg);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            return toResponse<FeedResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
        }

        /// <summary>
        /// 税航开票反写接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_JS_INVOICE")]
        public IActionResult JinShuiInvoiceTOOracleERP([FromBody] string stringjson)
        {
            StringBuilder strMsg = new StringBuilder();
            //string result = string.Empty;
            JSResponseDto result = new JSResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToOracleERP.JinShuiInvoiceToERP(stringjson, strMsg);
                if (string.IsNullOrEmpty(strMsg.ToString()))
                {
                    statuscode = "S";
                }
                else
                {
                    statuscode = "E";
                }
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            //return toResponse(StatusCodeType.Success, result + strMsg.ToString());
            return toResponse<JSResponseDto>(StatusCodeType.Success, strMsg.ToString(), result);
        }

        /// <summary>
        /// 自动获取MES投料数据同步接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult GetMESTLToERP([FromBody] string strjson)
        {
            StringBuilder strMsg = new StringBuilder();
            List<MEStlDto> obj = new List<MEStlDto>();
            ERPTlResponseDto result = new ERPTlResponseDto();
            string statuscode = string.Empty;
            try
            {
                result = _ToOracleERP.GetMESTLToERP(strjson, strMsg);
                if (string.IsNullOrEmpty(strMsg.ToString()))
                {
                    statuscode = "S";
                }
                else
                {
                    statuscode = "E";
                }
               
            }
            catch (Exception er )
            {
                strMsg.Append(er.Message.ToString());
            }
            //[20211104 shb update] 返回方式调整 start
            return toResponse<ERPTlResponseDto>(StatusCodeType.Success, strMsg.ToString(),result); 
        }

        /// <summary>
        /// 不成功数据，循环投料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetTestClob([FromBody] string strjson)
        {
            StringBuilder strMsg = new StringBuilder();
            //List<MEStlDto> obj = new List<MEStlDto>();
            ERPTlResponseDto result = new ERPTlResponseDto();
            //string statuscode = string.Empty;
            try
            {
                result = _ToOracleERP.GetTestClob(strjson, strMsg);
                //if (string.IsNullOrEmpty(strMsg.ToString()))
                //{
                //    statuscode = "S";
                //}
                //else
                //{
                //    statuscode = "E";
                //}
                strMsg.Append("已关闭此接口！");
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            //[20211104 shb update] 返回方式调整 start
            return toResponse(StatusCodeType.Success, strMsg.ToString()); ;
        }

        /// <summary>
        /// 测试用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorization(Power = "PRIV_ERP_ALL")]
        public IActionResult RecieveOraData([FromBody] string strjson)
        {
            StringBuilder strMsg = new StringBuilder();
          
            try
            {
                //_logger.LogTrace(strjson);
                Meiam.System.Common.Helpers.LogHelpers.WriteLogTXT(strjson);
            }
            catch (Exception er)
            {
                strMsg.Append(er.Message.ToString());
            }
            //[20211104 shb update] 返回方式调整 start
            return toResponse(StatusCodeType.Success, strMsg.ToString()); ;
        }


    }
}
